package com.starbaba.baidublack.view;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.baidublack.model.BlackDetail;
import com.starbaba.baidublack.model.BlackInfo;
import com.starbaba.baidublack.service.BaiduBlackService;
import com.starbaba.baidublack.service.impl.BaiduBlackServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public class BaiduBlackAction extends BaseAction implements ModelDriven<BlackInfo> {

	/**
	 * 百度黑名单action
	 */
	private static final long serialVersionUID = 1L;
	BaiduBlackService blackService = new BaiduBlackServiceImpl();
	private String fileName;
	private BlackInfo blackInfo = new BlackInfo();;
	private InputStream input;

	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}

	/**
	 * 黑名单列表
	 */
	public void getBlackList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		List<BlackInfo> list = blackService.getBaiduBlackList(pageRequest, pageResponse, paramsMap);
		for (int i = 0; i <list.size(); i++) {
			BlackInfo bi = list.get(i);
			bi.setMobnum(PhoneNumNideUtil.hideNum(bi.getMobnum()));
			list.set(i,bi);
		}
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			System.out.println(JsonUtils.toJson(pageResponse, null, true, null, false));
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 导出黑名单信息列表
	 * */
	public String exportDetailCVS() throws UnsupportedEncodingException {

		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);

		pageRequest.setIDisplayStart(-1);
		pageRequest.setIDisplayLength(-1);
		pageRequest.setOrderby("createtime");
		pageRequest.setOrder("desc");
		List<BlackInfo> list = blackService.getBaiduBlackList(pageRequest, pageResponse, paramsMap);
		String value = blackService.exportBaiduBlackCVS(list);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}

	public String getBlackDetail() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap); // 将请求参数封装带paramsMap中
		String id = (String) paramsMap.get("data_id");
		String userid = (String) paramsMap.get("data_userid");
		BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
		String sql = "select blackReason,blackdetail from loan_user_black where id=? and userid=?";
		try {
			blackInfo = baseDao.get(sql, BlackInfo.class, id, userid);
		} catch (AppException e) {
			e.printStackTrace();
		}
		String reason = blackInfo.getBlackReason();
		String detailList = blackInfo.getBlackdetail();
		List<BlackDetail> list = getBlackDetailList(reason, detailList);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "success";
	}

	public List<BlackDetail> getBlackDetailList(String reason, String detailList) {
		List<BlackDetail> blackDetails = new ArrayList<BlackDetail>();
		if (!StringUtils.isEmpty(reason)) {
			if (!StringUtils.isEmpty(reason)) {
				String[] reasons = reason.split(":");
				JsonObject json = new JsonParser().parse(detailList).getAsJsonObject();
				for (int i = 0; i < reasons.length; i++) {
					if (reasons[i] != null && !"".equals(reasons[i])) {
						// 法院失信
						if (reasons[i].equals("C06BT020")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();
								String duty = object.get("duty").getAsString();
								if (!StringUtils.isEmpty(duty)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院失信");
									blackDetail.setTitleName("执行义务");
									blackDetail.setTitleDes(duty);
									blackDetails.add(blackDetail);
								}

								String performance = object.get("performance").getAsString();
								if (!StringUtils.isEmpty(performance)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院失信");
									blackDetail.setTitleName("执行情况");
									blackDetail.setTitleDes(performance);
									blackDetails.add(blackDetail);
								}

								String publishDate = object.get("publishDate").getAsString();
								if (!StringUtils.isEmpty(publishDate)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院失信");
									blackDetail.setTitleName("公开日期");
									blackDetail.setTitleDes(publishDate);
									blackDetails.add(blackDetail);
								}

								String case_no = object.get("case_no").getAsString();
								if (!StringUtils.isEmpty(case_no)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院失信");
									blackDetail.setTitleName("案件编号");
									blackDetail.setTitleDes(case_no);
									blackDetails.add(blackDetail);
								}

								String disruptTypeName = object.get("disruptTypeName").getAsString();
								if (!StringUtils.isEmpty(disruptTypeName)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院失信");
									blackDetail.setTitleName("违反条例");
									blackDetail.setTitleDes(disruptTypeName);
									blackDetails.add(blackDetail);
								}
							}
						}

						// 偷税漏税
						if (reasons[i].equals("C06BT021")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();

								String taxpayer_name = object.get("taxpayer_name").getAsString();
								if (!StringUtils.isEmpty(taxpayer_name)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("偷税漏税");
									blackDetail.setTitleName("税务主体");
									blackDetail.setTitleDes(taxpayer_name);
									blackDetails.add(blackDetail);
								}

								String taxpayer_iden_num = object.get("taxpayer_iden_num").getAsString();
								if (!StringUtils.isEmpty(taxpayer_iden_num)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("偷税漏税");
									blackDetail.setTitleName("纳税识别号");
									blackDetail.setTitleDes(taxpayer_iden_num);
									blackDetails.add(blackDetail);
								}

								String info_source = object.get("info_source").getAsString();
								if (!StringUtils.isEmpty(info_source)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("偷税漏税");
									blackDetail.setTitleName("公开机构");
									blackDetail.setTitleDes(info_source);
									blackDetails.add(blackDetail);
								}

								String major_fact = object.get("major_fact").getAsString();
								if (!StringUtils.isEmpty(major_fact)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("偷税漏税");
									blackDetail.setTitleName("主要原因");
									blackDetail.setTitleDes(major_fact);
									blackDetails.add(blackDetail);
								}
							}
						}

						// 股权冻结
						if (reasons[i].equals("C06BT022")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();
								String execute_court = object.get("execute_court").getAsString();
								if (!StringUtils.isEmpty(execute_court)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("股权冻结");
									blackDetail.setTitleName("执行法院");
									blackDetail.setTitleDes(execute_court);
									blackDetails.add(blackDetail);
								}

								String adjudicate_no = object.get("adjudicate_no").getAsString();
								if (!StringUtils.isEmpty(adjudicate_no)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("股权冻结");
									blackDetail.setTitleName("处罚编号");
									blackDetail.setTitleDes(adjudicate_no);
									blackDetails.add(blackDetail);
								}

								String freeze_amount = object.get("freeze_amount").getAsString();
								if (!StringUtils.isEmpty(freeze_amount)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("股权冻结");
									blackDetail.setTitleName("冻结金额");
									blackDetail.setTitleDes(freeze_amount);
									blackDetails.add(blackDetail);
								}

								String pub_date = object.get("pub_date").getAsString();
								if (!StringUtils.isEmpty(pub_date)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("股权冻结");
									blackDetail.setTitleName("处罚公开时间");
									blackDetail.setTitleDes(pub_date);
									blackDetails.add(blackDetail);
								}

								String corp_name = object.get("corp_name").getAsString();
								if (!StringUtils.isEmpty(corp_name)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("股权冻结");
									blackDetail.setTitleName("合作企业名称");
									blackDetail.setTitleDes(corp_name);
									blackDetails.add(blackDetail);
								}
							}
						}

						// 工商无照
						if (reasons[i].equals("C06BT023")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();
								String punishment_no = object.get("punishment_no").getAsString();
								if (!StringUtils.isEmpty(punishment_no)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("工商无照");
									blackDetail.setTitleName("处罚编号");
									blackDetail.setTitleDes(punishment_no);
									blackDetails.add(blackDetail);
								}

								String cause_of_action = object.get("cause_of_action").getAsString();
								if (!StringUtils.isEmpty(cause_of_action)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("工商无照");
									blackDetail.setTitleName("执法原因");
									blackDetail.setTitleDes(cause_of_action);
									blackDetails.add(blackDetail);
								}

								String handle_org_name = object.get("handle_org_name").getAsString();
								if (!StringUtils.isEmpty(handle_org_name)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("工商无照");
									blackDetail.setTitleName("处罚部门");
									blackDetail.setTitleDes(handle_org_name);
									blackDetails.add(blackDetail);
								}

								String pub_date = object.get("pub_date").getAsString();
								if (!StringUtils.isEmpty(pub_date)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("工商无照");
									blackDetail.setTitleName("公示日期");
									blackDetail.setTitleDes(pub_date);
									blackDetails.add(blackDetail);
								}

								String punishment_content = object.get("punishment_content").getAsString();
								if (!StringUtils.isEmpty(punishment_content)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("工商无照");
									blackDetail.setTitleName("处罚内容");
									blackDetail.setTitleDes(punishment_content);
									blackDetails.add(blackDetail);
								}
							}
						}

						// 法院执行人
						if (reasons[i].equals("C06BT024")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();
								String reg_date = object.get("reg_date").getAsString();
								if (!StringUtils.isEmpty(reg_date)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院执行人");
									blackDetail.setTitleName("注册时间");
									blackDetail.setTitleDes(reg_date);
									blackDetails.add(blackDetail);
								}

								String case_no = object.get("case_no").getAsString();
								if (!StringUtils.isEmpty(case_no)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院执行人");
									blackDetail.setTitleName("案件编号");
									blackDetail.setTitleDes(case_no);
									blackDetails.add(blackDetail);
								}

								String court = object.get("court").getAsString();
								if (!StringUtils.isEmpty(court)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院执行人");
									blackDetail.setTitleName("判决法院");
									blackDetail.setTitleDes(court);
									blackDetails.add(blackDetail);
								}

								String exec_money = object.get("exec_money").getAsString();
								if (!StringUtils.isEmpty(exec_money)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("法院执行人");
									blackDetail.setTitleName("执行金额");
									blackDetail.setTitleDes(exec_money);
									blackDetails.add(blackDetail);
								}
							}
						}

						// 执行处罚
						if (reasons[i].equals("C06BT025")) {
							JsonArray array = json.get(reasons[i]).getAsJsonArray();
							for (int j = 0; j < array.size(); j++) {
								JsonObject object = array.get(i).getAsJsonObject();
								String cf_wsh = object.get("cf_wsh").getAsString();
								if (!StringUtils.isEmpty(cf_wsh)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("行政处罚决定书文号");
									blackDetail.setTitleDes(cf_wsh);
									blackDetails.add(blackDetail);
								}

								String cf_cfmc = object.get("cf_cfmc").getAsString();
								if (!StringUtils.isEmpty(cf_cfmc)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("处罚名称");
									blackDetail.setTitleDes(cf_cfmc);
									blackDetails.add(blackDetail);
								}

								String cf_sy = object.get("cf_sy").getAsString();
								if (!StringUtils.isEmpty(cf_sy)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("处罚事由");
									blackDetail.setTitleDes(cf_sy);
									blackDetails.add(blackDetail);
								}

								String cf_jg = object.get("cf_jg").getAsString();
								if (!StringUtils.isEmpty(cf_jg)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("处罚结果");
									blackDetail.setTitleDes(cf_jg);
									blackDetails.add(blackDetail);
								}

								String cf_xzjg = object.get("cf_xzjg").getAsString();
								if (!StringUtils.isEmpty(cf_xzjg)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("处罚机关");
									blackDetail.setTitleDes(cf_xzjg);
									blackDetails.add(blackDetail);
								}

								String reg_date = object.get("reg_date").getAsString();
								if (!StringUtils.isEmpty(reg_date)) {
									BlackDetail blackDetail = new BlackDetail();
									blackDetail.setTitleType("行政处罚");
									blackDetail.setTitleName("更新时间");
									blackDetail.setTitleDes(reg_date);
									blackDetails.add(blackDetail);
								}
							}
						}

					} else {
						BlackDetail blackDetail = new BlackDetail();
						blackDetail.setTitleType("信用良好");
						blackDetail.setTitleName("信用良好");
						blackDetail.setTitleDes("信用良好");
						blackDetails.add(blackDetail);
					}
				}
			}
		} else {
			BlackDetail blackDetail = new BlackDetail();
			blackDetail.setTitleType("信用良好");
			blackDetail.setTitleName("信用良好");
			blackDetail.setTitleDes("信用良好");
			blackDetails.add(blackDetail);
		}
		return blackDetails;
	}

	@Override
	public BlackInfo getModel() {
		if (blackInfo == null) {
			blackInfo = new BlackInfo();
		}
		return blackInfo;
	}

}
